package syllabus.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import syllabus.commons.ExtGridReturn;
import syllabus.commons.ExtPager;
import syllabus.commons.ExtReturn;
import syllabus.model.Criteria;
import syllabus.model.PFile;
import syllabus.model.PSyllabus;
import syllabus.service.PFileService;
import syllabus.service.PSyllabusService;
import syllabus.shiro.ShiroDbRealm;

@RequestMapping("/checkSyllabus")
@Controller
public class CheckSyllabusAct {

	private static final Logger logger = LoggerFactory.getLogger(CheckSyllabusAct.class);
	
	@Autowired
	private PSyllabusService syllabusService;
	@Autowired
	private PFileService fileService;
	
	@RequestMapping("/list")
	public String list(){
		return "/syllabus/checkList";
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	@ResponseBody
	public Object all(ExtPager pager,
			HttpServletRequest request,ModelMap map) {

		Criteria criteria = new Criteria();
		// 设置分页信息
		if (pager.getPage() != null && pager.getRows() != null) {
			criteria.setPage(pager.getPage());
			criteria.setPageSize(pager.getRows());
		}
		// 排序信息
		if (StringUtils.isNotBlank(pager.getOrder())
				&& StringUtils.isNotBlank(pager.getSort())) {
			criteria.setOrderByClause(pager.getSort() + " " + pager.getOrder());
		}
		Subject suser = SecurityUtils.getSubject();
		ShiroDbRealm.ShiroUser shiroUser = (ShiroDbRealm.ShiroUser) suser.getPrincipal();
		//增加限定条件
		if (suser.hasRole("user")) {
			criteria.put("uploadUser", shiroUser.loginName);
		}
		//List<PSyllabus> list = this.syllabusService.selectByExample(criteria);
		List<PFile> list = fileService.getAttachAndSyllabusName(criteria);
		
		int total = this.fileService.countBySyllabus(criteria);
		
		return new ExtGridReturn(total, list);
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public void editAttach(PFile file,String attachId,HttpServletResponse response){
		ExtReturn ext = new ExtReturn();
		Integer id = file.getId();
		if(StringUtils.isNotBlank(attachId)){
			//新上传文件
			PFile pf = fileService.selectByPrimaryKey(Integer.parseInt(attachId));
			
			//更新原有文件记录
			file.setFileName(pf.getFileName());
			file.setFilePath(pf.getFilePath());
			fileService.updateByPrimaryKeySelective(file);
			
			ext.setSuccess(ExtReturn.SUCCESS);
			ext.setMsg("上传成功");
			//删除新上传文件
			fileService.deleteByPrimaryKey(pf.getId());
		}
		else{
			ext.setSuccess(ExtReturn.FAILURE);
			ext.setMsg("上传失败");
		}
		
		response.setContentType("text/plain;charset=utf-8");
		ObjectMapper om = new ObjectMapper();
		try {
			om.writeValue(response.getWriter(),ext );
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public Object check(String attachId,String tg){
		ExtReturn ext = new ExtReturn();
		if(StringUtils.isNotBlank(attachId)){
			PFile pf = fileService.selectByPrimaryKey(Integer.parseInt(attachId));
			if(StringUtils.isNotBlank(tg)){
				if(tg.equals(PFile.TG_SUCCESS)){
					pf.setTg(PFile.TG_SUCCESS);
				}else if(tg.equals(PFile.TG_EDIT)) {
					pf.setTg(PFile.TG_EDIT);
				}
			}
			
			fileService.updateByPrimaryKeySelective(pf);
			
			ext.setSuccess(ExtReturn.SUCCESS);
			ext.setMsg("操作成功");
		}
		else{
			ext.setSuccess(ExtReturn.FAILURE);
			ext.setMsg("操作失败");
		}
		return ext;
	}
}
